/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.TimingScore;
import com.boxin.ims.modules.wechat.entity.TimingScoreLog;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.dao.TimingScoreLogDao;

/**
 * 定点扫描获得积分日志Service
 * @author Jakemanse
 * @version 2014-05-09
 */
@Component
@Transactional(readOnly = true)
public class TimingScoreLogService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TimingScoreLogService.class);
	
	@Autowired
	private TimingScoreLogDao timingScoreLogDao;
	
	public TimingScoreLog get(Long id) {
		return timingScoreLogDao.findOne(id);
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-5-15  下午11:45:22
	 * @function <p>  </p>
	 * @param timingScore
	 * @param member
	 */
	public void saveFromTimeScore(TimingScore timingScore,WechatMember member){
		TimingScoreLog  tlog = new TimingScoreLog();
		tlog.setDay(DateUtils.getTodayString());
		tlog.setMember(member);
		tlog.setName(member.getName());
		tlog.setTimingScore(timingScore);
		tlog.setUser(timingScore.getUser());
		
		timingScoreLogDao.save(tlog);
	}
	
	
	
	public List<TimingScoreLog> find(TimingScoreLog timingScoreLog){
		
		DetachedCriteria dc = timingScoreLogDao.createDetachedCriteria();
		dc.createAlias("member", "member");
		dc.createAlias("timingScore", "timingScore");
		
		if (timingScoreLog.getUser()!=null && timingScoreLog.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", timingScoreLog.getUser().getId()));
		}
		
		
		if (timingScoreLog.getMember()!=null && timingScoreLog.getMember().getId()>0){
			dc.add(Restrictions.eq("member.id", timingScoreLog.getMember().getId()));
		}
		
		
		
		if (timingScoreLog.getTimingScore()!=null && timingScoreLog.getTimingScore().getId()>0){
			dc.add(Restrictions.eq("timingScore.id", timingScoreLog.getTimingScore().getId()));
		}
		
		
		if(com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(timingScoreLog.getDay())){
			dc.add(Restrictions.eq("day", timingScoreLog.getDay()));
		}
		
		
		if (StringUtils.isNotEmpty(timingScoreLog.getName())){
			dc.add(Restrictions.like("name", "%"+timingScoreLog.getName()+"%"));
		}
		
		
		return timingScoreLogDao.find( dc);
		
	}
	
	
	public int findCount(TimingScoreLog timingScoreLog){
		List<TimingScoreLog> list = find(timingScoreLog);
		return (list!=null && !list.isEmpty())?list.size():0;
	}
	
	
	
	public Page<TimingScoreLog> find(Page<TimingScoreLog> page, TimingScoreLog timingScoreLog) {
		DetachedCriteria dc = timingScoreLogDao.createDetachedCriteria();
		if (timingScoreLog.getUser()!=null && timingScoreLog.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", timingScoreLog.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(timingScoreLog.getName())){
			dc.add(Restrictions.like("name", "%"+timingScoreLog.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return timingScoreLogDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(TimingScoreLog timingScoreLog) {
		timingScoreLogDao.save(timingScoreLog);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		timingScoreLogDao.deleteById(id);
	}
	
}
